package com.guodefu.demo.controller;

import com.guodefu.demo.domain.DictionaryDO;
import com.guodefu.demo.dto.DictionaryDTO;
import com.guodefu.demo.mapstruct.DictionaryMapStructMapper;
import com.guodefu.demo.service.DictionaryService;
import com.guodefu.demo.vo.DictionaryVO;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Validated
@RestController
@RequestMapping("/dict")
public class DictionaryController {

    private final DictionaryMapStructMapper mapStructMapper;

    private final DictionaryService dictionaryService;

    public DictionaryController(DictionaryMapStructMapper mapStructMapper, DictionaryService dictionaryService) {
        this.mapStructMapper = mapStructMapper;
        this.dictionaryService = dictionaryService;
    }

    @GetMapping()
    public DictionaryVO getByTypeAndCode(@NotNull(message = "type不能为null") String type,
                                         @NotNull(message = "code不能为null") String code) {
        DictionaryDO dictDO = dictionaryService.getOneByTypeAndCode(type, code);
        return mapStructMapper.toVO(dictDO);
    }

    @GetMapping("/{id}")
    public DictionaryVO getById(@PathVariable @NotNull(message = "ID不能为null") Long id) {
        DictionaryDO dictDO = dictionaryService.getOneById(id);
        return mapStructMapper.toVO(dictDO);
    }

    @PutMapping("/{id}")
    public DictionaryVO update(@PathVariable @NotNull(message = "ID不能为null") Long id,
                               @Valid @RequestBody DictionaryDTO dict) {
        DictionaryDO dictDO = dictionaryService.update(id, dict);
        return mapStructMapper.toVO(dictDO);
    }

    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Long id) {
        DictionaryDO dictionary = dictionaryService.getOneById(id);
        return dictionaryService.delete(dictionary);
    }

    @PostMapping
    public DictionaryVO add(@Valid @RequestBody DictionaryDTO dict) {
        DictionaryDO dictDO = dictionaryService.insert(dict);
        return mapStructMapper.toVO(dictDO);
    }

    @GetMapping("/type/{type}")
    public List<DictionaryVO> getListByType(@PathVariable String type) {
        List<DictionaryDO> dictDOList = dictionaryService.getListByType(type);
        return mapStructMapper.toVOList(dictDOList);
    }

    @GetMapping("/searchByName")
    public List<DictionaryVO> getByNameOrType(@NotNull(message = "name不能为null")  String name,
                                              @RequestParam(required = false) String type) {
        List<DictionaryDO> dictDOList = dictionaryService.getByNameOrType(name, type);
        return mapStructMapper.toVOList(dictDOList);
    }

}

